//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Core
{
    using System;
    using System.Configuration;

    public abstract class PreflightConfigurationSectionBase : ConfigurationSection
    {
        public bool IsPropertyValueSupplied<TProperty>(string propertyName) where TProperty : IComparable<TProperty>
        {
            var type = typeof(TProperty);
            if (type.IsClass)
            {
                return this.GetValue<TProperty>(propertyName) != null;
            }

            return this.GetValue<TProperty>(propertyName).Equals(default(TProperty)) == false;
        }

        public TProperty GetValue<TProperty>(string propertyName) where TProperty : IComparable<TProperty>
        {
            return (TProperty)this[propertyName];
        }

        public void InvokeActionIfPropertyAvailable<TProperty>(string propertyName, Action<TProperty> setValueAction) where TProperty : IComparable<TProperty>
        {
            if (this.IsPropertyValueSupplied<TProperty>(propertyName))
            {
                var value = this.GetValue<TProperty>(propertyName);
                Log.LogVerbose("Property: {0} is defined in configuration and has value: {1}", propertyName, value);
                setValueAction(value);
            }
        }

        protected abstract void SetProperties();

        protected void AddConfigurationProperty<TProperty>(string name, ConfigurationPropertyOptions options = ConfigurationPropertyOptions.None) where TProperty : IComparable<TProperty>
        {
            var configurationProperty = new ConfigurationProperty(name, typeof(TProperty), default(TProperty), options);
            this.Properties.Add(configurationProperty);
        }

        protected override void Init()
        {
            this.SetProperties();
            base.Init();
        }
    }
}
